#include "PlotColors.h"

PlotColors::PlotColors()
{
	clearColors();
	this->setFixedSize(2*(size+space)+space,2*(size+space)+space);
	showClassifC11=false;
}
void PlotColors::clearColors()
{
	colors.clear();
	selec.clear();
}
void PlotColors::addColors(QColor c)
{
	colors+=c;
	selec+=false;
	
	int d=round(sqrt(colors.size()));
	setFixedSize(d*(size+space)+space,d*(size+space)+space);
	update();
}

void PlotColors::paintEvent(QPaintEvent * e)
{
	QPainter painter(this);
	
	painter.setBrush(QColor(0,0,0));
	painter.drawRect(rect());
	
	int d=round(sqrt(colors.size()));
	
	for(int i=0;i<colors.size();i++)
	{
		painter.setBrush(QColor(colors[i].red(),colors[i].green(),colors[i].blue(),255));
		painter.setPen(QColor(0,0,0));
		
		painter.drawRect(QRect(space+(i/d)*(size+space),
							   space+(i%d)*(size+space),
							   size,
							   size));
		
		if(selec[i])
		{
			painter.setBrush(Qt::NoBrush);
			painter.setPen(QColor(255,255,255));
			painter.drawRect(QRect(space+(i/d)*(size+space),
								   space+(i%d)*(size+space),
								   size,
								   size));
		}
		
		if(showClassifC11==true)
		{
			painter.setBrush(getC11Color(colors[i].alpha()));
			painter.drawRect(QRect(space+(i/d)*(size+space),
							   space+(i%d)*(size+space),
							   size/10,
							   size/10));
		}
	}
}
void PlotColors::mousePressEvent(QMouseEvent * event)
{
	int d=round(sqrt(colors.size()));
	for(int i=0;i<colors.size();i++)
	{
		QRect rectangle(space+(i/d)*(size+space),space+(i%d)*(size+space),size,size);
		
		if(rectangle.contains(event->x(),event->y()))
		{
			selec[i]=!selec[i];
		}
	}
	
	update();
}

void PlotColors::setColors(int color1,int color2)
{
	this->color1=color1;
	this->color2=color2;
}

QString PlotColors::getResults()
{
	QString results("");

	for(int i=0;i<colors.size();i++)
	{
		results+=QString::number(colors[i].red())+tr(";")+
				 QString::number(colors[i].green())+tr(";")+
				 QString::number(colors[i].blue())+tr(";")+
				 QString::number(selec[i]*color2+(1-selec[i])*(-1))+tr(";")+
				 QString::number(colors[i].alpha())+tr("\n");
	}
	
	return results;
}		

void PlotColors::displayClassif(bool b)
{
	showClassifC11=b;
}

